package com.xiaoxx.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoxx.annotation.LogRecords;
import com.xiaoxx.common.base.JwtUser;
import com.xiaoxx.common.result.R;
import com.xiaoxx.model.vo.sys.SysMsgListVo;
import com.xiaoxx.model.vo.sys.SysMsgQuery;
import com.xiaoxx.model.vo.sys.SysMsgVo;
import com.xiaoxx.service.SysMsgService;
import com.xiaoxx.utils.JwtUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * base--sys--消息通知 前端控制器
 * </p>
 *
 * @author HX
 * @since 2023-08-03
 */
@RestController
@RequestMapping("/api/sys/msg")
public class SysMsgController {
    @Resource
    private SysMsgService msgService;
    @Resource
    private HttpServletRequest request;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "消息列表")
    @ApiResponses(value = {@ApiResponse(response = SysMsgListVo.class, code = 200, message = "响应成功")})
    public R<IPage<SysMsgListVo>> list(@RequestBody SysMsgQuery query) {
        JwtUser jwtUser = JwtUtil.getJwtUser(request);
        query.setUserId(jwtUser.getUserId());
        return R.success(msgService.listMsg(query));
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "消息id", required = true, dataType = "String", paramType = "query")})
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @LogRecords(moduleName = "系统信息", content = "删除消息记录")
    public R<Boolean> del(@RequestParam String id) {
        //删除消息发送记录
        Boolean b = msgService.removeById(id);
        return R.success(b);
    }


    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "消息id", required = true, dataType = "String", paramType = "query")})
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    public R<SysMsgVo> findById(@RequestParam String id) {
        SysMsgVo vo=msgService.findById(id);
        return R.success(vo.convert(SysMsgVo.class));
    }

    @RequestMapping(value = "/del/read", method = RequestMethod.GET)
    @ApiOperation(value = "删除已读", notes = "删除已读")
    @LogRecords(moduleName = "系统信息", content = "删除已读")
    public R<Boolean> delRead() {
        return R.success(msgService.delRead(JwtUtil.getJwtUser(request).getUserId()));
    }

    @RequestMapping(value = "/readAll", method = RequestMethod.GET)
    @ApiOperation(value = "全部已读", notes = "全部已读")
    @LogRecords(moduleName = "系统信息", content = "全部已读")
    public R<Boolean> readAll() {
        return R.success(msgService.readAll(JwtUtil.getJwtUser(request).getUserId()));
    }

    @GetMapping("/notReadCount")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "获取未读消息数量成功")})
    @ApiOperation(value = "未读消息数量", notes = "未读消息数量")
    public R<Integer> getNotReadCount() {
        return R.success(msgService.getNotReadCount(JwtUtil.getJwtUser(request).getUserId()));
    }
}

